package com.feicheng;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class TimeSlotGenerator {

    public static JSONObject generateTimeSlots(LocalDateTime st, LocalDateTime et) {
        JSONObject result = new JSONObject();

        // 每天的10分钟单位数
        int slotsPerDay = 24 * 6; // 24小时 * 6个10分钟

        // 遍历从st到et的每一天
        LocalDateTime currentDate = st;
        while (currentDate.isBefore(et) || currentDate.isEqual(et)) {
            LocalDateTime startOfDay = currentDate.with(LocalTime.MIN);

            // 生成当天的字符串
            StringBuilder dayString = new StringBuilder();
            for (int i = 0; i < slotsPerDay; i++) {
                LocalDateTime slotTime = startOfDay.plusMinutes(i * 10);
                if ((slotTime.isAfter(st) || slotTime.isEqual(st)) && (slotTime.isBefore(et) || slotTime.isEqual(et))) {
                    dayString.append("1");
                } else {
                    dayString.append("0");
                }
            }

            // 将日期作为key，字符串作为value放入JSONObject
            String dateKey = currentDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
            result.put(dateKey, dayString.toString());

            // 移动到下一天
            currentDate = currentDate.plusDays(1);
        }

        return result;
    }

    public static void main(String[] args) {
        LocalDateTime st = LocalDateTime.of(2023, 10, 1, 8, 0); // 2023-10-01 08:00
        LocalDateTime et = LocalDateTime.of(2023, 10, 3, 12, 0); // 2023-10-03 12:00

        JSONObject timeSlots = generateTimeSlots(st, et);
        System.out.println(timeSlots.toString()); // 格式化输出JSON
    }


}
